package com.yunfeng.supercall.activity;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lin.utils.Session;
import com.ly.quickdev.library.activity.DevBaseActivity;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX.Req;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.app.AppContext;
import com.yunfeng.supercall.bean.Constants;
import com.yunfeng.supercall.bean.ShareConfigBean;
import com.yunfeng.supercall.bean.UserBean;
import com.yunfeng.supercall.httputils.HttpClient;
import com.yunfeng.supercall.manager.LoginManager;
import com.yzx.api.UCSCall;

/**
 * activity基类，提供一些通用方法
 * 
 * @author 洋
 * 
 */
public class BaseActivity extends DevBaseActivity implements Constants {

	public boolean isFirstUse() {
		SharedPreferences mPreferences = getSharedPreferences("first",
				MODE_PRIVATE);
		boolean first = mPreferences.getBoolean("first", true);
		mPreferences.edit().putBoolean("first", false).commit();
		return first;
	}

	public void showProgressDialog(int resId) {
		showProgressDialog(getString(resId));
	}

	public HttpClient mHttpClient = new HttpClient();
	/** 拨号面板上的字符 */
	public static final SparseArray<String> CHARS;
	static {
		CHARS = new SparseArray<String>();
		CHARS.put(1, "ABC");
		CHARS.put(2, "DEF");
		CHARS.put(3, "GHI");
		CHARS.put(4, "JKL");
		CHARS.put(5, "MNO");
		CHARS.put(6, "PQRS");
		CHARS.put(7, "TUV");
		CHARS.put(8, "WXYZ");
	}
	/** 云之讯通话状态码表 */
	public static final HashMap<Integer, String> dialState = new HashMap<Integer, String>();
	static {
		// dialState.put(UCSCall.CALL_VOIP_ANSWER, "接听电话");
		dialState.put(UCSCall.CALL_VOIP_NOT_ENOUGH_BALANCE, "余额不足");
		dialState.put(UCSCall.CALL_VOIP_BUSY, "对方正忙");
		dialState.put(UCSCall.CALL_VOIP_REFUSAL, "对方拒绝");
		dialState.put(UCSCall.CALL_VOIP_NUMBER_ERROR, "被叫号码不在线");
		dialState.put(UCSCall.CALL_VOIP_REJECT_ACCOUNT_FROZEN, "被号账户被冻结");
		dialState.put(UCSCall.CALL_VOIP_ACCOUNT_FROZEN, "账户被冻结");
		dialState.put(UCSCall.CALL_VOIP_ACCOUNT_EXPIRED, "主号账户过期");
		dialState.put(UCSCall.CALL_VOIP_CALLYOURSELF, "不能拨打自己绑定号码");
		dialState.put(UCSCall.CALL_VOIP_NETWORK_TIMEOUT, "呼叫请求超时");
		dialState.put(UCSCall.CALL_VOIP_NOT_ANSWER, "对方无人应答");
		dialState.put(UCSCall.CALL_VOIP_TRYING_183, "被叫不在线,转直拨");
		dialState.put(UCSCall.CALL_VOIP_RINGING_180, "对方正在响铃");
		dialState.put(UCSCall.CALL_VOIP_SESSION_EXPIRATION, "鉴权失败(TCP未认证)");
		dialState.put(UCSCall.CALL_VOIP_ERROR, "服务器错误");
		dialState.put(UCSCall.HUNGUP_NOT_ANSWER, "被叫方没有应答");
		dialState.put(UCSCall.HUNGUP_MYSELF, "自己挂断电话");
		dialState.put(UCSCall.HUNGUP_OTHER, "对方挂断电话");
		dialState.put(UCSCall.HUNGUP_REFUSAL, "对方拒绝接听");
		dialState.put(UCSCall.HUNGUP_NOT_ENOUGH_BALANCE, "余额不足");
		dialState.put(UCSCall.HUNGUP_MYSELF_REFUSAL, "自己拒绝接听");
		dialState.put(UCSCall.CALL_VIDEO_DOES_NOT_SUPPORT, "该设备不支持视频通话");
	}

	@Override
	public void onSupportContentChanged() {
		super.onSupportContentChanged();
		/** 标题栏右边的按钮 */
		final View right = findViewById(R.id.right_text);
		if (right != null) {
			right.setOnClickListener(this);
		}
	}

	/**
	 * 设置标题
	 * 
	 * @param title
	 */
	public void setTitleBar(CharSequence title) {
		TextView tv = findView(R.id.title);
		if (tv != null) {
			tv.setText(title);
		}
	}

	public void setTitleBar(int resId) {
		setTitleBar(getString(resId));
	}

	/**
	 * 获取基础的参数
	 * 
	 * @param context
	 * @return
	 */
	public static JSONObject getBaseParams(Context context) {
		JSONObject json = new JSONObject();
		try {
			UserBean u = getUser(context);
			if (u == null) {
				json.put("i_userid", 0);
			} else {
				json.put("i_userid", getUser(context).getUserid());
			}

			json.put("i_lng", AppContext.lng);
			json.put("i_lat", AppContext.lat);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return json;
	}

	/**
	 * 获取登陆管理器
	 * 
	 * @param context
	 * @return
	 */
	public static LoginManager getLoginManager(Context context) {
		try {
			return LoginManager.getInstance(context.getApplicationContext());
		} catch(NullPointerException e) {
			return LoginManager.getInstance(context);	
		}
	}

	/**
	 * 通过包名检测系统中是否安装某个应用程序
	 * 
	 * @param context
	 * @param packageName
	 *            ：应用程序的包名(QB:com.tencent.mtt)
	 * @return true : 系统中已经安装该应用程序
	 * @return false : 系统中未安装该应用程序
	 * */
	public static boolean checkApkExist(Context context, String packageName) {
		if (packageName == null || "".equals(packageName)) {
			return false;
		}
		try {
			context.getPackageManager().getApplicationInfo(packageName,
					PackageManager.GET_UNINSTALLED_PACKAGES);
			return true;
		} catch (NameNotFoundException e) {
			return false;
		}
	}

	/**
	 * 分享到微信
	 * 
	 * @param context
	 * @param scene
	 *            朋友圈or好友聊天
	 */
	public static void shareByWeiXin(Context context, int scene) {
		boolean hasWeXin = checkApkExist(context, "com.tencent.mm");
		if (!hasWeXin) {
			Toast.makeText(context, R.string.weichat_not_installed,
					Toast.LENGTH_SHORT).show();
			return;
		}
		IWXAPI mIwxapi = WXAPIFactory.createWXAPI(context, WEICHAT_KEY);
		mIwxapi.registerApp(WEICHAT_KEY);
		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = getLoginManager(context).getRegUrl();
		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = webpage;
		msg.title = ShareConfigBean.sConfig.getTitle();
		msg.description = ShareConfigBean.sConfig.getContent()
				+ getLoginManager(context).getRegUrl();
		Req mReq = new Req();
		mReq.transaction = buildTransaction("text"); // transaction字段用于唯一标识一个请求
		mReq.scene = scene;
		mReq.message = msg;
		mIwxapi.sendReq(mReq);
	}

	private static String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis())
				: type + System.currentTimeMillis();
	}

	/**
	 * 获取当前用户
	 * 
	 * @param context
	 * @return
	 */
	public static UserBean getUser(Context context) {
		Session mSession = Session.getSession();
		mSession.init(context, true);
		UserBean user = mSession.getmUserBean();
		return user;
	}

	// public void wakeUp(){
	// KeyguardManager keyguardManager =
	// (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
	// KeyguardLock keyguardLock = keyguardManager.newKeyguardLock(LOCK_TAG);
	// keyguardLock.disableKeyguard();
	// }

	// public void releaseWakeUp(){
	// keyguardLock.reenableKeyguard();
	// }
}
